package com.ckzp.jfinal.system.util;

import com.ckzp.core.Results;
import com.ckzp.jfinal.system.model.SysWorklog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2022/7/27.
 */
public class SysWorkLogUtil {

    /**
     * 日志池
     */
    private static List POOL;

    private static int ADD = 1;
    private static int GET = 2;


    /**
     * @return
     */
    public static SysWorkLogUtil n() {
        return new SysWorkLogUtil();
    }


    /**
     * 同步池
     *
     * @param types
     * @param worklog
     * @return
     */
    public synchronized static List syncWorklogPool(int types, SysWorklog worklog) {
        if (POOL == null) POOL = new ArrayList();
        if (types == ADD) {
            POOL.add(worklog);
            return null;
        } else {
            List list = POOL;
            POOL = new ArrayList();
            return list;
        }
    }

    /**
     * 写入日志
     *
     * @param empid
     * @param title
     * @param desc
     * @return
     */
    public Results saveLog(String empid, String title, String desc) {
        SysWorklog worklog = new SysWorklog().set("log_time", new Date()).set("emp_id", empid).set("log_title", title).set("log_desc", desc);
        syncWorklogPool(1, worklog);
        return Results.n();
    }

    /**
     * 写入日志
     *
     * @param empid
     * @param title
     * @param desc
     * @return
     */
    public Results saveLog(String empid, String key1, String title, String desc) {
        SysWorklog worklog = new SysWorklog().set("log_time", new Date()).set("key1", key1).set("emp_id", empid)
                .set("log_title", title).set("log_desc", desc);
        syncWorklogPool(1, worklog);
        return Results.n();
    }

    /**
     * 写入日志
     *
     * @param empid
     * @param title
     * @param desc
     * @return
     */
    public Results saveLog(String empid, String key1, String key2, String title, String desc) {
        SysWorklog worklog = new SysWorklog().set("log_time", new Date()).set("key1", key1).set("key2", key2)
                .set("emp_id", empid).set("log_title", title).set("log_desc", desc);
        syncWorklogPool(1, worklog);
        return Results.n();
    }

    /**
     * 写入日志
     *
     * @param empid
     * @param title
     * @param desc
     * @return
     */
    public Results saveLog(String empid, String key1, String key2, String title, String desc, String ip) {
        SysWorklog worklog = new SysWorklog().set("log_time", new Date()).set("key1", key1).set("key2", key2)
                .set("emp_id", empid).set("log_title", title).set("log_ip", ip).set("log_desc", desc);
        syncWorklogPool(1, worklog);
        return Results.n();
    }


    /**
     * 写入日志
     *
     * @param worklog
     * @return
     */
    public Results saveLog(SysWorklog worklog) {
        syncWorklogPool(1, worklog);
        return Results.n();
    }

    /**
     * 同步日志至数据库中
     *
     * @return
     */
    public int scyWorkLog() {
        int succ = 0;
        List<SysWorklog> list = syncWorklogPool(2, null);
        if (list != null) {
            for (SysWorklog worklog : list) {
                if (worklog.save()) succ++;
            }
        }
        return succ;
    }
}
